import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:hotpot/utils/time_utils.dart';
import 'z_text.dart';
import 'z_image.dart';

extension State2ToastExt on State {
  void showToast(String tip) => SmartDialog.showToast(tip);
}

extension Widget2WidgetExt on Widget {
  Widget paddingSymmetric({double horizontal = 0, double vertical = 0}) {
    return Padding(padding: EdgeInsets.symmetric(horizontal: horizontal.r, vertical: vertical.r), child: this);
  }

  Widget paddingOnly({double left = 0, double top = 0, double right = 0, double bottom = 0}) {
    return Padding(padding: EdgeInsets.only(left: left.r, top: top.r, right: right.r, bottom: bottom.r), child: this);
  }

  Widget align(Alignment alignment) {
    return Align(alignment: alignment, child: this);
  }

  Widget alignRight() {
    return Align(alignment: Alignment.centerRight, child: this);
  }

  Widget alignLeft() {
    return Align(alignment: Alignment.centerLeft, child: this);
  }

  Widget alignStart() {
    return Align(alignment: Alignment.topCenter, child: this);
  }

  Widget alignCenter() {
    return Align(alignment: Alignment.center, child: this);
  }

  Widget clipRRect(BorderRadius borderRadius) {
    return ClipRRect(
      borderRadius: borderRadius.r,
      child: this,
    );
  }

  Widget positioned({double? left, double? top, double? right, double? bottom}) {
    return Positioned(
      left: left?.r,
      top: top?.r,
      right: right?.r,
      bottom: bottom?.r,
      child: this,
    );
  }

  Widget popScope({bool canPop = false, PopInvokedCallback? onPopInvoked}) {
    return PopScope(
      canPop: canPop,
      onPopInvoked: onPopInvoked,
      child: this,
    );
  }

  Widget sizedBox({double? width, double? height}) {
    return SizedBox(width: width?.r, height: height?.r, child: this);
  }

  Widget expanded({int flex = 1}) {
    return Expanded(flex: flex, child: this);
  }

  Widget gestureDetector(
      {GestureTapCallback? onTap, GestureTapUpCallback? onTapUp, HitTestBehavior behavior = HitTestBehavior.opaque}) {
    return GestureDetector(
      onTap: () {
        onTap?.call();
        TimeUtils.startWait();
      },
      behavior: behavior,
      child: this,
    );
  }

  Widget opacity(double opacity) {
    return Opacity(opacity: opacity, child: this);
  }

  Widget visibility(bool visible) {
    return Visibility(visible: visible, child: this);
  }

  Widget inkWell({GestureTapCallback? onTap, Color? highlightColor, double radius = 0, ValueChanged<bool>? onHighlightChanged}) {
    return InkWell(
      onTap: onTap,
      highlightColor: highlightColor,
      onHighlightChanged: onHighlightChanged,
      borderRadius: BorderRadius.circular(radius.r),
      child: this,
    );
  }
}

extension Num2WidgetExt on num? {
  Widget get wSizedBox => SizedBox(width: this?.r);

  Widget get hSizedBox => SizedBox(height: this?.r);

  Widget line({Color? color, double? indent, double? endIndent, double? height = 0}) {
    return Divider(height: height?.r, color: color, indent: indent?.r, endIndent: endIndent?.r, thickness: this?.r);
  }

  Widget text({double size = 14, Color color = Colors.black, bool bold = false, double? def = 0, TextAlign? textAlign}) {
    return ZText(
      text: '${this ?? def}',
      size: size,
      textColor: color,
      textAlign: textAlign,
      fontWeight: bold ? FontWeight.bold : FontWeight.normal,
    );
  }
}

extension String2WidgetExt on String? {
  Widget text({double size = 14, Color color = Colors.black, bool bold = false, int maxLines = 1, TextAlign? textAlign}) {
    return ZText(
      text: this,
      size: size,
      textColor: color,
      fontWeight: bold ? FontWeight.bold : FontWeight.normal,
      maxLines: maxLines,
      textAlign: textAlign,
    );
  }

  Widget assetImage({double? width, double? height, BoxFit? fit}) {
    return ZImage(asset: this, fit: fit, width: width, height: height);
  }
}
